/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlterarEmpregado.java
 *
 * Created on 11/12/2011, 15:47:40
 */

package gui;

import DAO.DAOGenerico;
import DAO.DAOJSON;
import com.sun.corba.se.spi.orb.StringPair;
import empregado.Empregado;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonModel;
import javax.swing.JOptionPane;
import utils.StringParaData;
import validators.ValidatorError;
import validators.annotations.ValidateAnnotationParser;

/**
 *
 * @author thiago
 */
public class AlterarEmpregado extends javax.swing.JDialog {
    private GregorianCalendar dataNasc;
    private GregorianCalendar dataAdm;
    private GregorianCalendar dataDesl;
    private String cpf;
    private String nome;
    private String sexo;
    private Double salario;
    
    /** Creates new form AlterarEmpregado */
    public AlterarEmpregado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupsexo = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtcpf = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtnome = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        botaofeminino = new javax.swing.JRadioButton();
        botaomasculino = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        txtnascimento = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtadmissao = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtsalario = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtdesligamento = new javax.swing.JTextField();
        alterarButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Alterar Informações do Empregado");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(0, 51, 51));
        jLabel2.setText("Procurar cpf:");

        txtcpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcpfActionPerformed(evt);
            }
        });
        txtcpf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtcpfFocusLost(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(0, 51, 51));
        jLabel3.setText("Nome:");

        txtnome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnomeActionPerformed(evt);
            }
        });
        txtnome.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtnomeFocusLost(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel8.setForeground(new java.awt.Color(0, 51, 51));
        jLabel8.setText("Sexo:");

        buttonGroupsexo.add(botaofeminino);
        botaofeminino.setText("Feminino");
        botaofeminino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaofemininoActionPerformed(evt);
            }
        });

        buttonGroupsexo.add(botaomasculino);
        botaomasculino.setText("Masculino");
        botaomasculino.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaomasculinoActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(0, 51, 51));
        jLabel4.setText("Data de Nascimento:");

        txtnascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtnascimentoActionPerformed(evt);
            }
        });
        txtnascimento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtnascimentoFocusLost(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(0, 51, 51));
        jLabel5.setText("Data de Admissão:");

        txtadmissao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtadmissaoActionPerformed(evt);
            }
        });
        txtadmissao.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtadmissaoFocusLost(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setForeground(new java.awt.Color(0, 51, 51));
        jLabel6.setText("Salário");

        txtsalario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtsalarioActionPerformed(evt);
            }
        });
        txtsalario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtsalarioFocusLost(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setForeground(new java.awt.Color(0, 51, 51));
        jLabel7.setText("Data de Desligamento:");

        txtdesligamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtdesligamentoActionPerformed(evt);
            }
        });
        txtdesligamento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtdesligamentoFocusLost(evt);
            }
        });

        alterarButton.setText("Alterar Empregado");
        alterarButton.setEnabled(false);
        alterarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4))
                                .addGap(17, 17, 17)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtadmissao, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtnascimento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                    .addComponent(txtsalario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                    .addComponent(txtdesligamento, javax.swing.GroupLayout.Alignment.LEADING)))
                            .addComponent(alterarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(jLabel8))
                            .addGap(60, 60, 60)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtnome, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                                .addComponent(txtcpf, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(botaofeminino)
                                    .addGap(18, 18, 18)
                                    .addComponent(botaomasculino)))
                            .addGap(41, 41, 41)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtcpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtnome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaofeminino)
                    .addComponent(botaomasculino)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtnascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtadmissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtsalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtdesligamento, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(alterarButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtcpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcpfActionPerformed
  
}//GEN-LAST:event_txtcpfActionPerformed

    private void txtnomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnomeActionPerformed
     
}//GEN-LAST:event_txtnomeActionPerformed

    private void botaofemininoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaofemininoActionPerformed

}//GEN-LAST:event_botaofemininoActionPerformed

    private void botaomasculinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaomasculinoActionPerformed

}//GEN-LAST:event_botaomasculinoActionPerformed

    private void txtnascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtnascimentoActionPerformed
       
}//GEN-LAST:event_txtnascimentoActionPerformed

    private void txtadmissaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtadmissaoActionPerformed
     
}//GEN-LAST:event_txtadmissaoActionPerformed

    private void txtsalarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtsalarioActionPerformed
    
}//GEN-LAST:event_txtsalarioActionPerformed

    private void txtdesligamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtdesligamentoActionPerformed
       
}//GEN-LAST:event_txtdesligamentoActionPerformed

    private void alterarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarButtonActionPerformed
    cpf = txtcpf.getText();
    nome = txtnome.getText();
    setsexo();
    setnascimento(txtnascimento.getText());
    setadmissao(txtadmissao.getText());
    setsalario(txtsalario.getText());
    setdesligamento(txtdesligamento.getText());
        
    Empregado empr = new  Empregado(cpf,nome,sexo,dataNasc,dataAdm,salario,dataDesl);
   
    DAOGenerico cadastrar = new DAOJSON();
    try {
        ValidateAnnotationParser validar = new ValidateAnnotationParser();
        if(validar.validate(empr)){
            cadastrar.salvar(empr);
        }
        fechar();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro ao Alterar", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_alterarButtonActionPerformed

private void txtcpfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtcpfFocusLost
    Empregado emp = new Empregado();
    emp.setCpf(txtcpf.getText());
    DAOGenerico buscar = new DAOJSON();
    try{
        if(buscar.buscar(emp) != null){
            emp = (Empregado) buscar.buscar(emp);
            txtnome.setText(emp.getNome());
            txtnascimento.setText(converteDataString(emp.getDtnascimento()));
            txtadmissao.setText(converteDataString(emp.getDtadmissao()));
            txtsalario.setText(String.valueOf(emp.getSalario()));
            txtdesligamento.setText(converteDataString(emp.getDtdesligamento()));
            if("MASCULINO".equals(emp.getSexo())){
                botaomasculino.setSelected(true);
            }
            if("FEMININO".equals(emp.getSexo())){
                botaofeminino.setSelected(true);
            }
            alterarButton.setEnabled(true);
        }
        else{
            JOptionPane.showMessageDialog(this,"CPF não encontrado!", "Erro", JOptionPane.ERROR_MESSAGE);
            limparCampos();
        }
    }catch(Exception ex){
        JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_txtcpfFocusLost

public String converteDataString(Calendar data){
    int dia = data.get(Calendar.DATE);
    int mes = data.get(Calendar.MONTH) + 1;
    int ano = data.get(Calendar.YEAR);
    String dt = Integer.toString(dia) + "/" + Integer.toString(mes) + "/" + Integer.toString(ano);
    return dt;
}

private void txtnomeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtnomeFocusLost

}//GEN-LAST:event_txtnomeFocusLost

private void txtnascimentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtnascimentoFocusLost
     
}//GEN-LAST:event_txtnascimentoFocusLost

private void txtadmissaoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtadmissaoFocusLost
  
}//GEN-LAST:event_txtadmissaoFocusLost

private void txtsalarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtsalarioFocusLost
   
}//GEN-LAST:event_txtsalarioFocusLost

private void txtdesligamentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtdesligamentoFocusLost

}//GEN-LAST:event_txtdesligamentoFocusLost
  
    private void setnascimento(String nascimento){
        try{
            dataNasc = (GregorianCalendar) StringParaData.converter(nascimento);
        }catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void setadmissao(String admissao){
        try{
            dataAdm = (GregorianCalendar) StringParaData.converter(admissao);
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void setdesligamento(String desligamento){
        if (!desligamento.equals("")){
            try{
                dataDesl = (GregorianCalendar) StringParaData.converter(desligamento);
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this,ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void setsalario(String sal){
        try{
            salario = Double.parseDouble(sal);
        }catch(Exception ex){
            ex.getMessage();
        }
    }

    private void setsexo(){
        if(botaofeminino.isSelected()){
            sexo = "FEMININO";
        }
        if(botaomasculino.isSelected()){
            sexo = "MASCULINO";
        }
    }

    private void limparCampos(){
            System.out.println("yes");
            txtcpf.setText("");
            txtnome.setText("");
            txtnascimento.setText("");
            txtadmissao.setText("");
            txtsalario.setText("");
            txtdesligamento.setText("");
            alterarButton.setEnabled(false);
    }

    private void fechar() {
        int response = JOptionPane.showConfirmDialog(this,"Deseja alterar outro empregado?","Usuário alterado com sucesso", JOptionPane.YES_NO_OPTION);

        if(response == JOptionPane.YES_OPTION){
            limparCampos();
        }

        if(response == JOptionPane.NO_OPTION){
            System.out.println("no");
            this.setVisible(false);
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AlterarEmpregado dialog = new AlterarEmpregado(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarButton;
    private javax.swing.JRadioButton botaofeminino;
    private javax.swing.JRadioButton botaomasculino;
    private javax.swing.ButtonGroup buttonGroupsexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField txtadmissao;
    private javax.swing.JTextField txtcpf;
    private javax.swing.JTextField txtdesligamento;
    private javax.swing.JTextField txtnascimento;
    private javax.swing.JTextField txtnome;
    private javax.swing.JTextField txtsalario;
    // End of variables declaration//GEN-END:variables

}
